package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsTray;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 托盘信息表
 */
public class WmsTrayController extends BaseController {
	
	public void listPage() {
		setAttr("loc_id", getPara("locId"));
		setAttr("tp_tnum", getPara("tp_tnum"));
		render("list.html");
	}
	
	public void listData() {	
		List<Record> list = Db.find("select t.*,s.storage_name from wms_tray t,wms_location l,wms_storage s where t.of_loc=l.id and l.storage_no=s.id and t.of_loc=?",getPara("locId"));		
		
		Map<String, Object> dictDataLoc_id = WmsTray.me.getDictDataLoc_id();
		for(Record record : list) {
			String fieldName = "of_loc";
			if(dictDataLoc_id.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataLoc_id.get(record.get(fieldName).toString()));
			}
		}
		System.out.println(list);
		renderDatagrid(list, list.size());
	}	
	//一键生成托盘
	public void setTrayBatch() {
			int loc_id = getParaToInt("locId");
			int tp_num = getParaToInt("tp_tnum");
			List<WmsTray> modelList = new ArrayList<WmsTray>();
			for(int i= 1;i<tp_num+1;i++){
				modelList.add(new WmsTray().set("of_loc", loc_id).set("tp_no", "#"+i));	
			}			
			Db.batchSave(modelList, 1000);			
			renderSuccess();
		}
	//增加页面
	public void addPage() {
		setAttr("loc_id", getPara("locId"));
		setAttr("dictDataLoc_id", WmsTray.me.getDictDataLoc_id());
		setAttr("storage_name", WmsTray.me.getStorageBylocId(getParaToInt("locId")));
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsTray.class, "model").save();
		
		addOpLog("[托盘信息表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {		
		setAttr("model", WmsTray.me.findById(getPara("id")));
		setAttr("storage_name", WmsTray.me.getStorageBylocId(getParaToInt("locId")));
		setAttr("dictDataLoc_id", WmsTray.me.getDictDataLoc_id());
		render("update.html");
	}
	
	//修改
	public void update() {
		WmsTray model = WmsTray.me.findById(getPara("id"));
		//model.set("tp_no", getPara("model.tp_no"));
		model.set("mat_no", getPara("model.mat_no"));
		model.set("mat_name", getPara("model.mat_name"));
		model.set("mat_num", getPara("model.mat_num"));
		model.set("pro_date", getPara("model.pro_date"));
		model.set("in_time", getPara("model.in_time"));
		model.set("out_time", getPara("model.out_time"));
		model.set("create_user", getPara("model.create_user"));
		//model.set("of_loc", getPara("model.of_loc"));	
		model.set("realid", getPara("model.realid"));
		boolean ret=model.update();
		if(ret==true){
			addOpLog("[托盘信息表] 修改");
			renderSuccess();
		}
		else{
			renderFailed("数据有误，保存失败");
		}
		
	}
	
	//删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new WmsTray().set("id", id).delete();
			
		}
		
		addOpLog("[托盘信息表] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsTray model = WmsTray.me.findById(getParaToInt("id"));
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_tray", properties, symbols, values);
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("托盘编号（#）");
		clomuns.add("tp_no");
		headers.add("物料编号");
		clomuns.add("mat_no");
		headers.add("物料名称");
		clomuns.add("mat_name");
		headers.add("物料数量");
		clomuns.add("mat_num");
		headers.add("生产日期");
		clomuns.add("pro_date");
		headers.add("存入时间");
		clomuns.add("in_time");
		headers.add("离库时间");
		clomuns.add("out_time");
		headers.add("操作人员");
		clomuns.add("create_user");
		headers.add("所属库位");
		clomuns.add("of_loc");
		headers.add("所属仓库");
		clomuns.add("storage_name");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("托盘信息表");
		
		addOpLog("[托盘信息表] 导出cvs");
		render(csvRender);
	}
	//对外API借口，放入托盘操作
	
	public void putInTray(){	
				
//		System.out.println(getPara("of_loc")+getParaMap());
//		renderSuccess();
		boolean result = false;	
		try {
			WmsTray traymodel = WmsTray.me.findFirst("select * from wms_tray where tp_no=? and of_loc=?",getPara("tp_no"),getPara("of_loc"));
			
			traymodel.set("mat_no", getPara("mat_no"));
			traymodel.set("mat_name", getPara("mat_name"));
			traymodel.set("mat_num", getPara("mat_num"));
			traymodel.set("pro_date", getPara("pro_date"));
			traymodel.set("in_time", getPara("in_time"));
			//traymodel.set("out_time", getPara("out_time"));
			traymodel.set("create_user", getPara("create_user"));
			//traymodel.set("of_loc", getPara("of_loc"));		
			result=traymodel.update();
		} catch (Exception e) {
			
			renderFailed("调用错误");
		}	
		if(result==true){
			renderSuccess("入库成功");			
		}
		else{
			renderFailed("数据有误");
		}
			
		
	}
	//对外API接口，清空托盘数据
	public void getOutTray(){
//		System.out.println(getPara("tp_no"));
//		System.out.println(getPara("of_loc"));
		boolean result = false;
		try {
			WmsTray traymodel = WmsTray.me.findFirst("select * from wms_tray where tp_no=? and of_loc=?",getPara("tp_no"),getPara("of_loc"));
			//traymodel.set("tp_no",getPara("tp_no"));
			traymodel.set("mat_no", null);
			traymodel.set("mat_name",null);
			traymodel.set("mat_num", null);
			traymodel.set("pro_date", null);
			traymodel.set("in_time", null);
			//traymodel.set("out_time", "");
			traymodel.set("create_user", null);
			//traymodel.set("of_loc", getPara("of_loc"));		
			result=traymodel.update();
		} catch (Exception e) {
			renderFailed("调用错误");
		}	
		if(result==true){
			renderSuccess("出库成功");			
		}
		else{
			renderFailed("数据有误");
		}
	}
	
}
